//
//  RQViewModel.swift
//  RuiQue-Swift
//
//  Created by xiute on 2018/10/31.
//  Copyright © 2018 cuijin. All rights reserved.
//

import UIKit
import SliderComponent
import RQConfig

typealias CompleteGetNetData = () -> Void

class RQViewModel: NSObject, SliderProtocol {
    //MARK: -properties
    private var _sliderView: SliderView?
    lazy var allDataSource = Array<RQModel>()
    var groupDataSource: Dictionary<String, Array<RQModel>> = [:]
    var keys: Array<String> = []
    var completeHandleData: CompleteGetNetData?
    weak var context: ViewController?
    
    
    func getNetData() -> Void {
        _ = RQNetService.shareInstance.rq_get(urlString: "raw/8wyuMzqR", parameter: nil) { (success, error, task, response) in
            if success {
                self.allDataSource = []
                self.groupDataSource = [:]
                self.keys = []
                self.handleSuccessData(response)
                self.sliderView?.reloadData()
                if self.completeHandleData != nil {
                    self.completeHandleData!()
                }
            }else {
                //做一些打点 展示无网页面之类的操作
            }
        }
    }
    
    func didSelect(_ index: Int) {
        self.sliderView!.didSelect(index)
    }
    
    private func handleSuccessData(_ response: Any?) -> Void {
        let data: Dictionary = response as! Dictionary<String, Array<Dictionary<String, Any>>>
        let dataArray: Array<Dictionary<String, Any>> = data["data"]!
        for obj: Dictionary<String, Any> in dataArray {
            let model = RQModel(dataDic: obj)
            self.allDataSource.append(model)
            if model.quoteAsset != nil &&
                self.groupDataSource[model.quoteAsset!] == nil {
                var assetArray = Array<RQModel>()
                assetArray.append(model)
                self.groupDataSource.updateValue(assetArray, forKey: model.quoteAsset!)
                self.keys.append(model.quoteAsset!)
            }else if model.quoteAsset != nil &&
                self.groupDataSource[model.quoteAsset!] != nil {
                var assetArray = self.groupDataSource[model.quoteAsset!]
                assetArray?.append(model)
                self.groupDataSource.updateValue(assetArray!, forKey: model.quoteAsset!)
            }
        }
        print(self.groupDataSource)
        
        
    }
    
    
    
    //MARK: -设置sliderView
    var sliderView: SliderView? {
        get {
            return _sliderView
        }
        
        set {
            _sliderView = newValue
            _sliderView?.delegate = self;
        }
    }
    
    //MARK: -sliderView protocol
    func numberOfCount(in sliderView: SliderView) -> Int {

        return self.groupDataSource.count;
    }
    
    func slider(_ sliderView: SliderView, itemOf number: Int) -> UIViewController {
        let vc = RQContentViewController()
        vc.datasource = self.groupDataSource[self.keys[number]]
        vc.context = self;
        return vc
    }
    
    func slider(_ sliderView: SliderView, didScrollAt number: Int) {
        self.context?.topSliderView.didSelect(index: number)
    }
    
    
    
    
}
